import {ProColumns, ProTable} from "@ant-design/pro-components";
import {GenerateTableVO, getGenerateTablePage, removeGenerateTable} from "@/services/GenerateTableService";
import {Button, Popconfirm, Space} from "antd";
import React from "react";

export interface GenerateTableProps {
  onSave?: () => void;
  onUpdate?: (record: GenerateTableVO) => void;
  onEditColumn: (record: GenerateTableVO) => void;
}


const GenerateTable = (props: GenerateTableProps) => {

  /**
   * 列定义
   */
  const columns: ProColumns<GenerateTableVO>[] = [
    {title: 'id', dataIndex: "id"},
    {title: '业务注释', dataIndex: "businessComment"},
    {title: '业务名', dataIndex: "businessName"},
    {title: '表名', dataIndex: "tableName"},
    {title: '包名', dataIndex: "packageName"},
    {
      title: '操作', render: (_: any, record: GenerateTableVO) => [
        <Space key="operate">
          <Button type="link" size="small" onClick={() => props.onUpdate && props.onUpdate(record)}>编辑</Button>
          <Button type="link" href={`/api/generateTable/generate?businessId=${record.id}`} size="small">生成代码</Button>
          <Popconfirm
            title="确定要删除吗"
            onConfirm={() => {
              removeGenerateTable(record.id).then()
            }}
            okText="确认"
            cancelText="取消"
          >
            <Button type="link" danger size="small">删除</Button>
          </Popconfirm>
        </Space>
      ],
      hideInSearch: true
    }
  ]

  // 工具栏
  let actions = [
    props?.onSave && <Button key="create" type="primary" onClick={props.onSave}>添加</Button>
  ]

  return <>
    <ProTable columns={columns} request={getGenerateTablePage}
              toolbar={{
                actions: actions
              }
              }>

    </ProTable>
  </>
}

export default GenerateTable
